
//
//  WBTitleButton.swift
//  SinaWebo
//
//  Created by monica on 17/5/21.
//  Copyright © 2017年 oms. All rights reserved.
//

import UIKit

class WBTitleButton: UIButton {

    //如果title为nil,显示首页,否则显示箭头和昵称
    init(title: String?) {
        super.init(frame: CGRect())
        
        if title == nil {
            setTitle("首页", for: .normal)
        }else{
            setTitle(title!+" ", for: .normal)
            setImage(UIImage(named:"navigationbar_arrow_down"), for: .normal)
            setImage(UIImage(named:"navigationbar_arrow_up"), for: .selected)
        }
        
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray, for: .normal)
        sizeToFit()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //重新布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //判断titleLabel和imageView是否同时存在
        guard let titleLabel = titleLabel, let imageView = imageView else {
            return
        }
        
        //将label的x向左移动image的宽度
//        titleLabel.frame = titleLabel.frame.offsetBy(dx: -imageView.bounds.width, dy: 0)
//        //将image的x向右移动label的宽度
//        imageView.frame = imageView.frame.offsetBy(dx: titleLabel.bounds.width+5, dy: 0)
        titleLabel.frame.origin.x = 0
        imageView.frame.origin.x = titleLabel.frame.size.width

        
    }
   
}
